(function() {
		var serverUrl = QualityCheck.GPUD.ServiceConfig;
		var AjaxCrud = QualityCheck.AjaxCRUD;
		$(function() {
			var usercode = $("#user-name",parent.parent.document).attr("code");
			var userkey = $("#user-name",parent.parent.document).attr("key");
			var usertype = $("#user-name",parent.parent.document).attr("usertype");
			keyCheck(usercode,usertype,userkey,function(){
				getSchoolCount();
				initbar();
				initpie();
				init_bar();
			});
		});
		
		function getSchoolCount() {
			var userKey = $("#user-name",parent.parent.document).attr("key");
			var data = {
				cmd : "getSchoolCount",
				model : "maintain",
				userKey : userKey,
				data : {
				},
				remark : ""
			};

			AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
				if(res.result == 1 && res.data != undefined && res.data != ""){
					$("#school-count").text(res.data);
				}else{
					$("#school-count").text(0);
				}
			},null,null);
			
		}
		function initbar() {
			var userKey = $("#user-name",parent.parent.document).attr("key");
			var data = {
				cmd : "bar",
				model : "maintain",
				userKey : userKey,
				data : {
				},
				remark : ""
			};

			$.ajax({
				type: "get",
				url : serverUrl.ServerLoginUrl, 
				dataType : 'json',//以 JSON方式 解释 
				contentType:"application/json; charset=utf-8",
				data:{param:JSON.stringify(data)},
				success: function(res){
					var content = "";
					if(res.result == 1 && res.data != undefined && res.data != ""){
						var arr = res.data.data.toString();
						arr = arr.split(",");
						
						content += 	"<tr  align='center' ><td height='36px'>学生</td>" +
						"<td height='36px'>" + arr[0] + "</td>" +
						"</tr>";
						content += 	"<tr  align='center' ><td height='36px'>校星卡</td>" +
						"<td height='36px'>" + arr[1] + "</td>" +
						"</tr>";
						
						// 基于准备好的dom，初始化echarts图表
						var myChart = echarts.init(document.getElementById('main'));

						var option = {
							title : {
								text : '学生和校星卡',
								x : 'center'
							},
							tooltip : {
								show : true
							},
							legend : {
								orient : 'vertical',
								x : 'left',
								data : [ "学生", "校星卡" ]
							},
							xAxis : [ {
								type : 'category',
								data : ["数量"]
							} ],
							yAxis : [ {
								type : 'value'
							} ],
							
							series : [ {
								"name" : "学生",
								"type" : "bar",
								"data" : [arr[0]],
								itemStyle:{
									normal: {
								        color: '#2ec7c9'
								    }
								},
							},{
								"name" : "校星卡",
								"type" : "bar",
								"data" : [arr[1]],
								itemStyle:{
									normal: {
								        color: '#b7a3df'
								    }
								},
							}]
						};

						// 为echarts对象加载数据
						myChart.setOption(option);
						
					}else{
						content = "<tr><td colspan='2' height='36px'>没有相关信息！</td></tr>";
					}
					$("#stu-table").append(content);
				}
			});
			
		}
			

		function initpie() {
			var userKey = $("#user-name",parent.parent.document).attr("key");
			var data = {
				cmd : "pie",
				model : "maintain",
				userKey : userKey,
				data : {
				},
				remark : ""
			};
			
			$.ajax({
				type: "get", 
				url : serverUrl.ServerLoginUrl, 
				dataType : 'json',//以 JSON方式 解释 
				contentType:"application/json; charset=utf-8",
				data:{param:JSON.stringify(data)},
				success: function(res){
					var content = "";
					if(res.result == 1 && res.data != undefined && res.data != ""){
						var arr = res.data.data;
						//var pie = "";
						for(var i=0;i<arr.length;i++){
							for(var key in data[i]){
									if(data[i][key] == null || data[i][key] == "null"){
										data[i][key] = "";
									}
								}
							content += 	"<tr  align='center' ><td height='36px'>" + arr[i].name + "</td>" +
							"<td height='36px'>" + arr[i].value + "</td>" +
							"</tr>";
							
						}
						
						// 基于准备好的dom，初始化echarts图表
						var myChart = echarts.init(document.getElementById('pie-main'));

						var option = {
							title : {
								text : '用户组成',
								x : 'center'
							},
							tooltip : {
								trigger : 'item',
								formatter : "{a} <br/>{b} : {c} ({d}%)"
							},
							legend : {
								orient : 'vertical',
								x : 'left',
								data : [ '教师', '家长', '管理' ]
							},
							color:['#4286f3','#ea4325','#f9bb04'],
							series : [ {
								name : '用户组成',
								type : 'pie',
								radius : '55%',
								center : [ '50%', '60%' ],
								data : (function() {
										var dataarr = [];
										dataarr = arr;
										return dataarr;
									})()
								} ]
								};

						// 为echarts对象加载数据
						myChart.setOption(option);
						
					}else{
						content = "<tr><td colspan='2' height='36px'>没有相关信息！</td></tr>";
					}
					$("#user-table").append(content);
				}
			});
		}

		function init_bar() {
			var userKey = $("#user-name",parent.parent.document).attr("key");
			var data = {
				cmd : "teacher_bar",
				model : "maintain",
				userKey : userKey,
				data : {
				},
				remark : ""
			};
			
			$.ajax({
				type: "get", 
				url : serverUrl.ServerLoginUrl, 
				dataType : 'json',//以 JSON方式 解释 
				contentType:"application/json; charset=utf-8",
				data:{param:JSON.stringify(data)},
				success: function(res){
					var content = "";
					if(res.result == 1 && res.data != undefined && res.data != ""){
						var arr = res.data.data.toString();
						arr = arr.split(",");
						
						content += 	"<tr  align='center' ><td height='36px'>教师</td>" +
						"<td height='36px'>" + arr[0] + "</td>" +
						"</tr>";
						content += 	"<tr  align='center' ><td height='36px'>学生</td>" +
						"<td height='36px'>" + arr[1] + "</td>" +
						"</tr>";

						// 基于准备好的dom，初始化echarts图表
						var myChart = echarts.init(document.getElementById('bar-main'));
						
						var option = {
							title : {
								text : '教师和学生',
								x : 'center'
							},
							tooltip : {
								show : true
							},
							legend : {
								orient : 'vertical',
								x : 'left',
								data : [ "教师", "学生" ]
							},
							xAxis : [ {
								type : 'category',
								data : [ "数量" ]
							} ],
							yAxis : [ {
								type : 'value'
							} ],
							series : [ {
								"name" : "教师",
								"type" : "bar",
								"data" : [arr[0]],
								itemStyle:{
									normal: {
								        color: '#2ec7c9'
								    }
								},
							},{
								"name" : "学生",
								"type" : "bar",
								"data" : [arr[1]],
								itemStyle:{
									normal: {
								        color: '#b7a3df'
								    }
								},
							} ]
						};

						// 为echarts对象加载数据
						myChart.setOption(option);
					}else{
						content = "<tr><td colspan='2' height='36px'>没有相关信息！</td></tr>";
					}
					$("#sch-table").append(content);
				}
			});
		}
})();